# -*- coding: utf-8 -*-
# @Time    : 2020/6/20 上午12:29
# @Author  : caotian
# @FileName: convolution2.py
# @Software: PyCharm
import matplotlib.pyplot as plt
import paddle.fluid as fluid
from PIL import Image
from paddle.fluid.dygraph.nn import Conv2D
from paddle.fluid.initializer import NumpyArrayInitializer
import numpy as np
#img=Image.open('./images/section1/000000355610.jpg')
img=Image.open('./images/section1/000000098520.jpg')
with fluid.dygraph.guard():
    w=np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],dtype='float32')/8
    w=w.reshape([1,1,3,3])
    w=np.repeat(w,3,axis=1)
    conv=Conv2D(num_channels=3,num_filters=1,filter_size=[3,3],param_attr=fluid.ParamAttr(initializer=NumpyArrayInitializer(value=w)))
    x=np.array(img).astype('float32')
    x=np.transpose(x,(2,0,1))
    x=x.reshape(1,3,img.height,img.width)
    x=fluid.dygraph.to_variable(x)
    y=conv(x)
    out=y.numpy()
plt.figure(figsize=(20,10))
f=plt.subplot(121)
f.set_title('input image',fontsize=15)
plt.imshow(img)
f=plt.subplot(122)
f.set_title('output feature map',fontsize=15)
plt.imshow(out.squeeze(),cmap='gray')
plt.show()
